const mysqlHelper = require('../db/mysql-helper.js')

/* eslint no-return-await:0 */
class SysUserModel {
  async findUserProfile() {
    // Similar: return await query('select * from user where uid = ?', uid);
    return await {
      name: '淘小宝',
      department: '技术部',
      avatar:
        'https://img.alicdn.com/tfs/TB1L6tBXQyWBuNjy0FpXXassXXa-80-80.png',
      userid: 10001,
    };
  }

  /**
   * 根据Id和密码得到数量
   * @param {} args
   */
  async getCountByIdAndPasswd ( args ) {
    let sql = 'SELECT COUNT(1) as resoutCount FROM SYS_User WHERE 1=1 AND id = ? AND `password` = ? '
    let params = [args.id, args.oldpasswd]
    let result = await mysqlHelper.query(sql, params)
    return result
  }

   /**
   * 根据Id 修改密码
   * @param {} args
   */
  async setUserPasswdById ( args ) {
    let sql = 'UPDATE SYS_User SET `password`=? WHERE id = ? '
    let params = [args.password,args.id]
    let result = await mysqlHelper.query(sql, params)
    return result
  }

    /**
   * 增加一条数据
   * @param  {object} args  参数
   * @return {object}       结果
   */
  async add ( args ) {
    let sql = 'INSERT INTO SYS_User ( login_name, PASSWORD, user_name, phone, e_mail, org_id, create_time, remarks, org_level) VALUES (?,?,?,?,?,?,NOW(),?,?)'
    let params = [args.login_name, args.password,args.user_name,args.phone,args.e_mail,args.org_id,args.remarks,args.org_level]
    let result = await mysqlHelper.query(sql, params)
    return result
  }

  /**
   * 根据UserName得到一条数据
   * @param  {object} args  参数
   * @return {object}       结果
   */
  async getByUserName( args ){
    let sql = 'SELECT * FROM SYS_User WHERE login_name = ?'
    let params = [args.username]
    let result = await mysqlHelper.query(sql, params)
    return result
  }

   /**
   * 根据LoginName得到数量
   * @param  {object} args  参数
   * @return {object}       结果
   */
  async getCountByLoginName( args ){
    let sql = 'SELECT COUNT(1) AS result_count FROM SYS_User WHERE 1=1 AND login_name = ? '
    let params = [args.loginname]
    let result = await mysqlHelper.query(sql, params)
    return result
  }

   /**
   *根据Email得到数量
   * @param  {object} args  参数
   * @return {object}       结果
   */
  async getCountByEmail( args ){
    let sql = 'SELECT COUNT(1) AS result_count FROM SYS_User WHERE 1=1 AND e_mail = ? '
    let params = [args.email]
    let result = await mysqlHelper.query(sql, params)
    return result
  }

   /**
   *根据Phone得到数量
   * @param  {object} args  参数
   * @return {object}       结果
   */
  async getCountByPhone( args ){
    let sql = 'SELECT COUNT(1) AS result_count FROM SYS_User WHERE 1=1 AND phone = ? '
    let params = [args.phone]
    let result = await mysqlHelper.query(sql, params)
    return result
  }

   /**
   *根据用户Id得到数量
   * @param  {object} args  参数
   * @return {object}       结果
   */
  async getCountByUserId( args ){
    let sql = 'SELECT COUNT(1) AS result_count FROM SYS_User WHERE 1=1 AND id = ?'
    let params = [args.id]
    let result = await mysqlHelper.query(sql, params)
    return result
  }

  /**
   *根据用户Id修改用户信息
   * @param  {object} args  参数
   * @return {object}       结果
   */
  async setUserById( args ){
    let params = new Array();
    let sql = "UPDATE SYS_User SET "
    if(args.user_name != undefined){
      sql  = sql + " user_name = ? ,"
      params.push(args.user_name)
    }
    if(args.phone != undefined){
      sql  = sql + " phone = ? ,"
      params.push(args.phone)
    }
    if(args.e_mail != undefined){
      sql  = sql + " e_mail = ? ,"
      params.push(args.e_mail)
    }
    if(args.org_id != undefined){
      sql  = sql + " org_id = ? ,"
      params.push(args.org_id)
    }
    if(args.status != undefined){
      sql  = sql + " status = ? ,"
      params.push(args.status)
    }
    if(args.remarks != undefined){
      sql  = sql + " remarks = ? ,"
      params.push(args.remarks)
    }
    if(args.org_level != undefined){
      sql  = sql + " org_level = ? ,"
      params.push(args.org_level)
    }
    sql = (sql .substring(sql .length - 1) == ',') ? sql .substring(0, sql .length - 1) : sql ;
    sql = sql + " WHERE id = ? ";
    params.push(args.id)
    let result = await mysqlHelper.query(sql,params)
    return result
  }

  async getUserListByFilter( args ){
    let sql = "SELECT * FROM SYS_User  WHERE  1=1 "
    if(args.value != undefined && args.value.username != undefined){
      sql  = sql +" and user_name like '%" + args.value.username+"%' "
    }

    if(args.value != undefined && args.value.status != undefined){
      sql  = sql +" and status = " + args.value.status
    }

    if(args.value != undefined && args.value.level != undefined){
      sql  = sql +" and org_level = " + args.value.level
    }
    sql  = sql +" limit "+args.offset+" , "+args.limit

    let result = await mysqlHelper.query(sql)
    return result
  }
  async getUserCountByFilter( args ){
    let sql = "SELECT COUNT(1) AS total FROM SYS_User  WHERE  1=1 "
    if(args.value != undefined && args.value.username != undefined){
      sql  = sql +" and user_name like '%" + args.value.username+"%' "
    }

    if(args.value != undefined && args.value.status != undefined){
      sql  = sql +" and status = " + args.value.status
    }

    if(args.value != undefined && args.value.level != undefined){
      sql  = sql +" and org_level = " + args.value.level
    }

    let result = await mysqlHelper.query(sql)
    return result
  }

  async getYearListByFilter( args ){
    let sql = "SELECT * FROM Total_StaDev_Year  WHERE  1=1 "
    if(args.ymonth != undefined){
      sql  = sql +" and YMonth = " + args.ymonth
    }

    if(args.devicetype != undefined){
      sql  = sql +" and DeviceType = " + args.devicetype
    }
    sql  = sql +" limit "+args.offset+" , "+args.limit

    let result = await mysqlHelper.query(sql)
    return result
  }
  async getYearCountByFilter( args ){
    let sql = "SELECT COUNT(1) AS total FROM Total_StaDev_Year  WHERE  1=1 "
    if(args.ymonth != undefined){
      sql  = sql +" and YMonth = " + args.ymonth
    }

    if(args.devicetype != undefined){
      sql  = sql +" and DeviceType = " + args.devicetype
    }
    let result = await mysqlHelper.query(sql)
    return result
  }
}

module.exports = new SysUserModel();
